package yxy.cherry.battle.module.selector;

import yxy.cherry.battle.Fighter;
import yxy.cherry.battle.Team;
import yxy.cherry.battle.module.Position;

import java.util.Set;

@TargetSelectorType(Type = 36)
public class TargetSelector_36_自己这行 extends TargetSelector {

	@Override
	public Set<Fighter> target(Fighter actor, Team team, int param1, int param2) {
		Position position = actor.getPosition();
		Position targetPosition = genTargetPosition(position,team);

		if(param1 == 0){
			switch (targetPosition) {
				case FU:
				case BU: {
					Set<Fighter> set = getRow(targetPosition, team);
					return randomTarget(set, param1, param2);
				}
				case FM:
				case BM: {
					Set<Fighter> set = getRow(targetPosition, team);
					return randomTarget(set, param1, param2);
				}
				case FD:
				case BD: {
					Set<Fighter> set = getRow(targetPosition, team);
					return randomTarget(set, param1, param2);
				}
			}

		}else if(param1 == 1){
			Set<Fighter> set = getFighterByPosition(targetPosition,team);
			if (set.size()>0) {
				return set;
			}
			return set;
		}else{
			return null;
		}
		return null;
	}

	/**
	 * 根据施法者的位置先选定基准目标位置
	 * @param position
	 * @return
	 */
	private Position genTargetPosition(Position position,Team team) {
		switch (position) {
			case FU:
			case BU: {
				Position positions[] = {Position.FU,Position.FM,Position.FD,Position.BU,Position.BM,Position.BD};
				return genPosition(positions,team);
			}
			case FM:
			case BM: {
				Position positions[] = {Position.FM,Position.FU,Position.FD,Position.BM,Position.BU,Position.BD};
				return genPosition(positions,team);
			}
			case FD:
			case BD: {
				Position positions[] = {Position.FD,Position.FU,Position.FM,Position.BD,Position.BU,Position.BM};
				return genPosition(positions,team);
			}
		}
		return null;
	}

	/**
	 * 获取有英雄的位置
	 * @param positions
	 * @param team
	 * @return
	 */
	private Position genPosition(Position[] positions,Team team) {
		for(Position position:positions){
			if(team.getFighterByPosition(position)!= null && team.getFighterByPosition(position).is活着()){
				return position;
			}
		}
		return null;
	}

	/**
	 * 获取指定位置的英雄
	 * @param position1
	 * @param team
	 * @return
	 */
	private Set<Fighter> genOppositeDirectionTarget(Position[] position1,Team team) {
		for(Position position:position1){
			Set<Fighter> set = getFighterByPosition(position,team);
			if (set.size()>0){
				return set;
			}
		}
		return null;
	}

}